<?php
/**
 * SampleController.php
 * 
 * @category    Madoqua
 * @package     Module
 * @subpackage  Install
 */

/** base class */
require_once 'Madoqua/Controller/Action/Abstract.php';

/** lorem ipsum... and all the rest :) */
require_once 'Madoqua/Text/Filler.php';

/**
 * Install_SampleController
 * 
 * @category    Madoqua
 * @package     Module
 * @subpackage  Install
 */
class Install_SampleController extends Madoqua_Controller_Action_Abstract 
{
    /**
     * initialization
     *
     */
    public function init() {
        parent::init();
        
        $this->getHelper('viewRenderer')->setNoRender();
    }
    
    /**
     * sample data, not really all that useful :x
     */
    public function SampleDataAction() {
         $this->_modelLoader->load(array('Blog_Page', 'Blog_Post', 'Blog_Comment', 'Blog_Category'));
         $postModel = new Blog_Post();
         $pageModel = new Blog_Page();
         $commentModel = new Blog_Comment();
         $categoryModel = new Blog_Category();
         $postCategoryModel = new Blog_PostCategory();
         //models
         
         $postModel->delete('id > 0');
         $pageModel->delete('id > 0');
         $commentModel->delete('id > 0');
         $categoryModel->delete('id > 0');
         //reset model data
             
         Madoqua_Search_Indexer::reset();
         //reset search index
         
         $categoryIds = array();
         for ($x = 0; $x < 7; $x++) {
             $name = Naneau_Text_Filler::getWords(rand(1, 3));
             $data = array(
                'name' => $name,
                'slug' => Blog_Post::slugify($name),
                'description' => Naneau_Text_Filler::getParagraphs(1, false),
                'parent' => 0
             );
             $categoryIds[] = $categoryModel->insert($data);
         }
         
         for ($x = 0; $x < 50; $x++) {
            $time = time() - rand(1000, 9999999);
            $data = array(
                'title' => Naneau_Text_Filler::getWords(rand(1, 4)),
                'content' => Naneau_Text_Filler::getParagraphs(rand(2, 5), true),
                'intro' => Naneau_Text_Filler::getParagraphs(1),
                'posted' => $time
            );
            $key = $postModel->insert($data);
            
            $data = array(
                'post_id' => $key,
                'spam' => rand(0,1),
                'accepted' => rand(0,1),
                'posted' => $time + rand(0, 99999),
                'name' => Naneau_Text_Filler::getWords(rand(1, 3)),
                'email' => strtolower(Naneau_Text_Filler::getWords(1)) . '@' . strtolower(Naneau_Text_Filler::getWords(1)) . '.com',
                'content' => Naneau_Text_Filler::getParagraphs(1, true),
                'ip' => '127.0.0.1',
                'user_agent' => 'madoqua_filler'
            );
            $commentModel->insert($data);
         }
         //insert posts
         
         $posts = $postModel->fetchAll();
         foreach($posts as $post) {
             $includes = array();
             for ($x = 0; $x < count($categoryIds); $x++) {
                 if (rand(0,3) <= 1) {
                     $includes[] = $categoryIds[$x];
                 }
             }
             //random array of category ides
             
             for ($x = 0; $x < count($includes); $x++) {
                 $postCategoryModel->insert(array(
                    'post_id' => $post->id,
                    'category_id' => $includes[$x]
                 ));
             }
             //assign them all to the post
         }
         //link posts to categories
            
         $titles = array('About', 'Mission statement', 'Something Funny', 'Contact');
         for ($x = 0; $x < 4; $x++) {
            $data = array(
                'title' => $titles[$x],
                'content' => Naneau_Text_Filler::getParagraphs(rand(2, 5), true),
                'posted' => time()
            );
            $pageModel->insert($data);             
         }
         
        $cache = Madoqua_Cache_Action::get();
        //get the action cacher
        $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array(
        'controller_post',
        'controller_comment',
        'controller_page' 
        ));
        //clear all post controller caches
        
    }
}